package com.ophelisis.actions.menuMgmt;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ophelisis.persistence.dao.MenuDAO;
import com.ophelisis.persistence.dao.MenuAuthDAO;
import com.ophelisis.persistence.domain.MenuVO;
import com.ophelisis.persistence.domain.MenuAuthVO;

/** <pre>
 * Class : com.ophelisis.actions.menuMgmt.MenuAuthMgmt / MenuAuthMgmt.java
 * 	Define Class Object : 
 * <br>
 * </pre>
 * @author hlee / apexcns,Inc.
 * @version 1.0
 * @see javadoc
 * @since 2013. 07. 29.
 */
@Controller
public class MenuAuthMgmt {
	@Autowired
	MenuAuthDAO menuAuthDAO;
	
	@Autowired
	MenuDAO menuDAO;
	
	private List<MenuAuthVO> authList;
	private List<MenuVO> menuList;
	private String sResult = "";
	
	private Log log = LogFactory.getLog(this.getClass());
	private static final String _LogPrefixStr = "MenuMgmt Log> ";
	
	@RequestMapping(value="menuAuthMgmt.action")
	public ModelAndView menuAuthMgmt(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value="target", defaultValue="maMain") String sTarget,
			@RequestParam(value="_groupId", defaultValue="0") String sMenuGroupId,
			@RequestParam(value="_menuId", defaultValue="0") String sMenuId,
			@RequestParam(value="_menuNm", defaultValue="") String sMenuNm,
			@RequestParam(value="_menuAuth", defaultValue="0") String sUserMenuAuth,
			@RequestParam(value="_prevUpCode", defaultValue="") String sPrevUpCode,
			@RequestParam(value="_prevCode", defaultValue="") String sPrevCode,
			@RequestParam(value="isNew", defaultValue="Y") String isNew,
			@ModelAttribute MenuAuthVO menuAuthVO) throws Exception {
		ModelAndView mav = new ModelAndView();

		log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		log.debug(_LogPrefixStr + "|" + " MenuAuthMgmt called !!!");
		log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		
		try {
			// 공통 요소
			mav.addObject("_groupId", sMenuGroupId);
			mav.addObject("_menuId", sMenuId);
			mav.addObject("_menuNm", sMenuNm);
			mav.addObject("_menuAuth", sUserMenuAuth);
			mav.addObject("_prevUpCode", sPrevUpCode);
			mav.addObject("_prevCode", sPrevCode);
			
			String sAuthId = "AUTH_" + menuAuthVO.getAuthLevel() + menuAuthVO.getMenuId();
			menuAuthVO.setAuthId(sAuthId);
			
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("authId", sAuthId);
			params.put("authLevel", menuAuthVO.getAuthLevel());
			
			if (sTarget.equals("maMain")) {
				mav = returnMgmt("main", mav, params);
			} else if (sTarget.equals("maList")) {
				mav = returnMgmt("list", mav, params);
			} else if (sTarget.equals("maDetail")) {
				mav = returnMgmt("detail", mav, params);
			} else if (sTarget.equals("maSave")) {
				try {
					sResult = menuAuthDAO.setMenuAuthModify(menuAuthVO) > 0 ? "정상적으로 처리되었습니다." : "처리에 실패하였습니다.<BR/>관리자에게 문의하여 주십시오."; // 정보 업데이트
				} catch (Exception e) {
					log.error("DB 정보 획득 failed!, authId=" + menuAuthVO.getAuthId(), e);
				}
				mav.addObject("result", sResult);
				
				mav = returnMgmt("detail", mav, params);
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return mav;
	}
	
	public ModelAndView returnMgmt(
			@RequestParam(value="cmd", defaultValue="list") String cmd,
			@ModelAttribute ModelAndView mav,
			Map<String, Object> params) throws Exception {
		if (cmd.equals("main")) {
			mav.setViewName("/WEB-INF/jsp/menuMgmt/menuAuthMgmt.jsp");

			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
			log.debug(_LogPrefixStr + "|" + " MenuAuthMgmt Main-Page called !!!");
			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		} else if (cmd.equals("list")) {
			try {
				menuList = menuDAO.getMenuList(params);
			} catch (Exception e) {
				log.error("DB 정보 획득 failed!", e);
			}
			mav.addObject("menuList", menuList);
			mav.setViewName("/WEB-INF/jsp/menuMgmt/menuAuthMgmtList.jsp");

			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
			log.debug(_LogPrefixStr + "|" + " MenuAuthMgmt List-Page called !!!");
			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		} else if (cmd.equals("detail")) {
			try {
				authList = menuAuthDAO.getMenuAuthDetail(params); // 쿼리 결과
			} catch (Exception e) {
				log.error("DB 정보 획득 failed!", e);
			}
			mav.addObject("menuAuth", authList.isEmpty() ? "0" : authList.get(0).getMenuAuth());
			mav.setViewName("/WEB-INF/jsp/menuMgmt/menuAuthMgmtDetail.jsp");
			
			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
			log.debug(_LogPrefixStr + "|" + " MenuAuthMgmt Detail-Page called !!!");
			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		}
		
		return mav;
	}
}

